package com.jg.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jg.visitor.GlobalConst;
import com.jg.visitor.common.utils.DateTimeUtil;
import com.jg.visitor.common.utils.LoginHelper;
import com.jg.visitor.common.utils.FileUploadUtils;
import com.jg.visitor.config.exception.BaseException;
import com.jg.visitor.controller.dto.FileUploadRecordDTO;
import com.jg.visitor.domain.FileUploadRecord;
import com.jg.visitor.service.FileUploadRecordService;
import com.jg.visitor.mapper.FileUploadRecordMapper;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author cqj
*/
@Service
public class FileUploadRecordServiceImpl extends ServiceImpl<FileUploadRecordMapper, FileUploadRecord> implements FileUploadRecordService{

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FileUploadRecordDTO uploadImg(MultipartFile multipartFile) throws IOException {
        if(null == multipartFile){
            throw new BaseException(-1,"请选择文件进行上传！");
        }

        //计算文件hash值，避免重复上传相同文件
        String hashCode32 = FileUploadUtils.md5HashCode32(multipartFile.getInputStream());

        //查询有没有相同hash的文件
        FileUploadRecord oldSameFile = getOne(new QueryWrapper<FileUploadRecord>().eq("hash_code", hashCode32).select("top 1 *"));

        FileUploadRecord fileUploadRecord = new FileUploadRecord();

        fileUploadRecord.setUploadIp(LoginHelper.getLoginIp());
        fileUploadRecord.setOpenId(LoginHelper.getLoginVisitorOpenId());
        fileUploadRecord.setUploadTime(DateTimeUtil.getTimeNow());
        fileUploadRecord.setHashCode(hashCode32);

        //如果有旧文件直接取旧文件，否则新写入磁盘
        if(oldSameFile != null){
            fileUploadRecord.setFilePath(oldSameFile.getFilePath());
            fileUploadRecord.setFileExt(oldSameFile.getFileExt());
            fileUploadRecord.setFileSize(oldSameFile.getFileSize());
            fileUploadRecord.setFileName(oldSameFile.getFileName());
        }
        //上传文件得到路径
        else{
            String path = FileUploadUtils.uploadPicture(multipartFile);
            fileUploadRecord.setFilePath(path);
            fileUploadRecord.setFileExt(FilenameUtils.getExtension(multipartFile.getOriginalFilename()));
            fileUploadRecord.setFileSize(multipartFile.getSize() + "");
            fileUploadRecord.setFileName(multipartFile.getOriginalFilename());
        }

        fileUploadRecord.insert();

        FileUploadRecordDTO dto = new FileUploadRecordDTO();
        BeanUtils.copyProperties(fileUploadRecord,dto);

        return dto;
    }

    @Override
    public String getFilePathById(List<FileUploadRecord> allImg, Long fileId){
        return allImg.stream().filter(img -> img.getId().equals(fileId)).map(FileUploadRecord::getFilePath).findAny().orElse(null);
    }
}




